<?php


namespace App\Http\Controllers\User\userCenter;


use App\Http\Controllers\Controller;
use App\Services\BasePayService;
use App\Services\GameRechargeService;
use App\Services\MemberService;
use App\Services\RechargeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * Class RechargeController
 * @package App\Http\Controllers\User\userCenter
 */
class RechargeController extends Controller
{
    /**
     * 获取充值后的奖励信息
     */
    public function getRechargeReward(Request $request)
    {
        $money = $request->get('money');
        $user = $request->userInfo;
        $integral = RechargeService::getGiftIntegral($user->id, $money);
        return successResp([
            'ly' => $money * 100,
            'integral' => $integral['integral'],
            'level_wf' => $integral['level_wf'],
            'rule_ly' => $integral['gift_ly'],
            'gift_wf' => $integral['gift_wf']
        ]);
    }

    /**
     * 充值支付
     */
    public function rechargePay(Request $request)
    {
        //验证表单
        $this->validate($request, [
            'money' => ['required','int'],
            'pay_type' => ['required','string'],
        ]);
        $user = $request->userInfo;
        $res = RechargeService::startPay($user->id, $request->money, $request->pay_type);
        return successResp($res);
    }

    /**
     * 回调
     */
    public function notify(Request $request)
    {
        $param = $request->all();
        Log::info('payNotify', $param);
        try {
            if ($param['remark'] == 1) {
                RechargeService::notify($param);
            } elseif ($param['remark'] == 3) {
                GameRechargeService::notify($param);
            }
        } catch (\Exception $e) {
            Log::error('payNotifyErr:',[$e->getMessage(), $param]);
            return 'error';
        }
        return 'success';
    }

    /**
     * 获取充值记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRechargeLog(Request $request)
    {
        $user = $request->userInfo;
        $list = DB::table('user_balance_log')
            ->where('unionid', $user->unionid)
            ->where('balance_type', MemberService::BalanceTypeLy)
            ->orderBy('id', 'desc')
            ->limit(30)
            ->get();
        foreach ($list as $v) {
            $v->create_time = date('Y-m-d H:i:s', $v->create_time);
        }
        return successResp($list);
    }

    /**
     * 获取充值记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRechargeLogByUser(Request $request)
    {
        $list = DB::table('user_recharge_order')
            ->where('user_id', $request->id)
            ->where('pay_status', 1)
            ->orderBy('id', 'desc')
            ->limit(30)
            ->get();
        foreach ($list as $v) {
            $v->create_time = date('Y-m-d H:i:s', $v->create_time);
        }
        return successResp($list);
    }

    /**
     * 获取订单状态
     * @param $type
     * @param $orderId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrderStatus($type, $orderId)
    {
        if ($type == 1) {
            $orderInfo = DB::table('user_recharge_order')->find($orderId);
        }
        if ($type == 3) {
            $orderInfo = DB::table('game_recharge_order')->find($orderId);
        }
        return successResp(['status' => !empty($orderInfo) ? $orderInfo->pay_status : 2]);
    }

    /**
     * 订单支付跳转页
     * @param int $orderId
     * @return mixed
     */
    public function payPage(int $type, int $orderId)
    {
        $key = BasePayService::getPayPageCacheKey($orderId, $type);
        $page = Cache::get($key);
        Cache::forget($key);
        return response($page);
    }

    public function testNotify(Request $request)
    {
        if ($request->get('id')) {
            RechargeService::notify(['ordernumber' => $request->get('id'), 'status'=>1, 'tradeno' => 'test_'.time()]);
        }
        return 'success';
    }

    /**
     * 充值规则
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRules()
    {
        $list = DB::table('recharge_rule')->orderBy('need_money', 'asc')->get();
        return successResp($list);
    }
}
